<?php

namespace App\Http\Controllers\Admin;

use App\CodeResponse;
use App\Http\Controllers\Controller;
use App\Models\Certificate;
use App\Models\Question;
use App\Models\QuestionOption;
use App\Services\CertificateService;
use App\Services\QuestionService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class QuestionController extends  AdminController
{
    //保存
    public function save(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'integer|digits_between:1,20|min:1',
            'name' => 'required|string',
            'stem' => 'required|string',
            'score' => 'required|numeric',
            'question_category_id' => 'required|integer|digits_between:1,20|min:1',
            'question_type' => 'integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }

        $putData = $request->except(['_method', '_token', 'option_content']);
        $users_id = Auth::id();
        if(isset($users_id)){
            $putData['users_id'] =  Auth::id()  ; //用户id
        }

        $id = $request->input('id');
        $flight = Question::updateOrCreate(
            ['id' => $id],
            $putData
        );
        $optionContentArr = $request->input('option_content');
        $optionContentArr = is_array($optionContentArr) ? $optionContentArr : json_decode($optionContentArr,true);
        $questionId = $flight->id;
        $questionOption = QuestionOption::where('question_id', $questionId);
        $questionOption->delete();
        if(is_array($optionContentArr)){
            foreach ($optionContentArr as $v ){
                $v['question_id'] = $questionId;
                $v['option_content'] = $v['option_content'] ;
                $v['order'] = $v['order'] ;
                $v['is_correct'] = $v['is_correct'] ;
                QuestionOption::create($v);
            }
        }
        return $this->success($questionId);
    }
    //列表
    public function  list(Request $request){
        $validator = Validator::make($request->all(), [
            'name' => 'string',
            'page' => 'integer',
            'limit' => 'integer',
            'sort' => 'string',
            'order' => 'string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $name = $request->input('name');
        $question_category_id = $request->input('question_category_id');

        $page = $request->input('page',1);
        $limit = $request->input('limit',10);
        $sort = $request->input('sort','created_at');
        $order = $request->input('order','asc');

        $list = QuestionService::getInstance()->list($page,$limit,$sort,$order, $name,$question_category_id);
        return $this->success($list);
    }
//删除
    public function delete(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $question = Question::find($request->id);
        $question->delete();
        $questionOption = QuestionOption::where('question_id',$request->id);
        $questionOption->delete();
        return $this->success();
    }
    //获取信息
    public function getInfo(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $info =QuestionService::getInstance()->info($request->id);
        return $this->success($info);
    }
    // 图上传
    public function img(Request $request)
    {
        $url = config('filesystems.disks.question.url');
        $default = config('filesystems.disks.question.default');
        $file = CertificateService::getInstance()->upfile($request, $url, $default, 'question','img');
        if(!$file){
            return $this->fail(CodeResponse::IMAGE_INCORRECT_FORMAT,'不支持当前图片扩展名');
        }
        return $this->success([
                'file' => $file,
            ]
        );
    }
}
